#####################################################################
# Figure 1
#####################################################################

rm(list=ls())

library(Hmisc)
library(exactRankTests)
library(ggplot2)
library(reshape2)
library(xtable)
library(stargazer)
library(sensitivitymw)
library(sensitivitymv)
library(lmtest)
library(sandwich)
library(stargazer)
library(msm)

# Read data and prepare basic output
load("~/Dropbox/Crime Brazil/06_replication/001_rematch_crime.Rdata")

#############################################
# Standardized differences before matching
#############################################

# mom 1
num0_mom1 = abs(mean(d$v2_bairrosafe_wave1_imp[d$t_ind==1])- mean(d$v2_bairrosafe_wave1_imp[d$t_ind==0]))
den0_mom1 = sqrt(  ( (sd(d$v2_bairrosafe_wave1_imp[d$t_ind==1]))^2 + (sd(d$v2_bairrosafe_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom1 = num0_mom1/den0_mom1
sd0_mom1

# mom 2
num0_mom2 = abs(mean(d$v7a_mediatvyesorno_wave3_imp[d$t_ind==1])- mean(d$v7a_mediatvyesorno_wave3_imp[d$t_ind==0]))
den0_mom2 = sqrt(  ( (sd(d$v7a_mediatvyesorno_wave3_imp[d$t_ind==1]))^2 + (sd(d$v7a_mediatvyesorno_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom2 = num0_mom2/den0_mom2
sd0_mom2

# mom 3
num0_mom3 = abs(mean(d$v9a_mediapaperyesorno_wave3_imp[d$t_ind==1])- mean(d$v9a_mediapaperyesorno_wave3_imp[d$t_ind==0]))
den0_mom3 = sqrt(  ( (sd(d$v9a_mediapaperyesorno_wave3_imp[d$t_ind==1]))^2 + (sd(d$v9a_mediapaperyesorno_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom3 = num0_mom3/den0_mom3
sd0_mom3

# mom 4
num0_mom4 = abs(mean(d$v10c_mediainternetfreq_wave3_imp[d$t_ind==1])- mean(d$v10c_mediainternetfreq_wave3_imp[d$t_ind==0]))
den0_mom4 = sqrt(  ( (sd(d$v10c_mediainternetfreq_wave3_imp[d$t_ind==1]))^2 + (sd(d$v10c_mediainternetfreq_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom4 = num0_mom4/den0_mom4
sd0_mom4

# mom 5
num0_mom5 = abs(mean(d$v16e_conversefriends_wave3_imp[d$t_ind==1])- mean(d$v16e_conversefriends_wave3_imp[d$t_ind==0]))
den0_mom5 = sqrt(  ( (sd(d$v16e_conversefriends_wave3_imp[d$t_ind==1]))^2 + (sd(d$v16e_conversefriends_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom5 = num0_mom5/den0_mom5
sd0_mom5

# mom 6
num0_mom6 = abs(mean(d$v16g_conversefamily_wave3_imp[d$t_ind==1])- mean(d$v16g_conversefamily_wave3_imp[d$t_ind==0]))
den0_mom6 = sqrt(  ( (sd(d$v16g_conversefamily_wave3_imp[d$t_ind==1]))^2 + (sd(d$v16g_conversefamily_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom6 = num0_mom6/den0_mom6
sd0_mom6

# mom 7
num0_mom7 = abs(mean(d$v20_comparetobairro_wave3_imp[d$t_ind==1])- mean(d$v20_comparetobairro_wave3_imp[d$t_ind==0]))
den0_mom7 = sqrt(  ( (sd(d$v20_comparetobairro_wave3_imp[d$t_ind==1]))^2 + (sd(d$v20_comparetobairro_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom7 = num0_mom7/den0_mom7
sd0_mom7

# mom 8
num0_mom8 = abs(mean(d$v30a_combat_crime_wave1[d$t_ind==1])- mean(d$v30a_combat_crime_wave1[d$t_ind==0]))
den0_mom8 = sqrt(  ( (sd(d$v30a_combat_crime_wave1[d$t_ind==1]))^2 + (sd(d$v30a_combat_crime_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom8 = num0_mom8/den0_mom8
sd0_mom8

# mom 9
num0_mom9 = abs(mean(d$v35a_attentionpres_wave2_imp[d$t_ind==1])- mean(d$v35a_attentionpres_wave2_imp[d$t_ind==0]))
den0_mom9 = sqrt(  ( (sd(d$v35a_attentionpres_wave2_imp[d$t_ind==1]))^2 + (sd(d$v35a_attentionpres_wave2_imp[d$t_ind==0]))^2 )/2  )
sd0_mom9 = num0_mom9/den0_mom9
sd0_mom9

# mom 11
num0_mom10 = abs(mean(d$v36a_persuadefromothersyesorno_wave3_imp[d$t_ind==1])- mean(d$v36a_persuadefromothersyesorno_wave3_imp[d$t_ind==0]))
den0_mom10 = sqrt(  ( (sd(d$v36a_persuadefromothersyesorno_wave3_imp[d$t_ind==1]))^2 + (sd(d$v36a_persuadefromothersyesorno_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom10 = num0_mom10/den0_mom10
sd0_mom10

# mom 11
num0_mom11 = abs(mean(d$v40a_thermmilitary_wave1_imp[d$t_ind==1])- mean(d$v40a_thermmilitary_wave1_imp[d$t_ind==0]))
den0_mom11 = sqrt(  ( (sd(d$v40a_thermmilitary_wave1_imp[d$t_ind==1]))^2 + (sd(d$v40a_thermmilitary_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom11 = num0_mom11/den0_mom11
sd0_mom11

# mom 12
num0_mom12 = abs(mean(d$v40b_thermCUT_wave1_imp[d$t_ind==1])- mean(d$v40b_thermCUT_wave1_imp[d$t_ind==0]))
den0_mom12 = sqrt(  ( (sd(d$v40b_thermCUT_wave1_imp[d$t_ind==1]))^2 + (sd(d$v40b_thermCUT_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom12 = num0_mom12/den0_mom12
sd0_mom12

# mom 13
num0_mom13 = abs(mean(d$v40d_thermbusiness_wave1_imp[d$t_ind==1])- mean(d$v40d_thermbusiness_wave1_imp[d$t_ind==0]))
den0_mom13 = sqrt(  ( (sd(d$v40d_thermbusiness_wave1_imp[d$t_ind==1]))^2 + (sd(d$v40d_thermbusiness_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom13 = num0_mom13/den0_mom13
sd0_mom13

# mom 14
num0_mom14 = abs(mean(d$v41b_thermfhc_wave3_imp[d$t_ind==1])- mean(d$v41b_thermfhc_wave3_imp[d$t_ind==0]))
den0_mom14 = sqrt(  ( (sd(d$v41b_thermfhc_wave3_imp[d$t_ind==1]))^2 + (sd(d$v41b_thermfhc_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom14 = num0_mom14/den0_mom14
sd0_mom14

# mom 15
num0_mom15 = abs(mean(d$v43a_pidyesorno_wave3[d$t_ind==1])- mean(d$v43a_pidyesorno_wave3[d$t_ind==0]))
den0_mom15 = sqrt(  ( (sd(d$v43a_pidyesorno_wave3[d$t_ind==1]))^2 + (sd(d$v43a_pidyesorno_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom15 = num0_mom15/den0_mom15
sd0_mom15

# mom 16
num0_mom16 = abs(mean(d$v46_pidimportant_wave1_imp[d$t_ind==1])- mean(d$v46_pidimportant_wave1_imp[d$t_ind==0]))
den0_mom16 = sqrt(  ( (sd(d$v46_pidimportant_wave1_imp[d$t_ind==1]))^2 + (sd(d$v46_pidimportant_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom16 = num0_mom16/den0_mom16
sd0_mom16

# mom 17
num0_mom17 = abs(mean(d$v50_ideology_wave3_imp[d$t_ind==1])- mean(d$v50_ideology_wave3_imp[d$t_ind==0]))
den0_mom17 = sqrt(  ( (sd(d$v50_ideology_wave3_imp[d$t_ind==1]))^2 + (sd(d$v50_ideology_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom17 = num0_mom17/den0_mom17
sd0_mom17

# mom 18
num0_mom18 = abs(mean(d$v69a_issuessocialspend_wave3_imp[d$t_ind==1])- mean(d$v69a_issuessocialspend_wave3_imp[d$t_ind==0]))
den0_mom18 = sqrt(  ( (sd(d$v69a_issuessocialspend_wave3_imp[d$t_ind==1]))^2 + (sd(d$v69a_issuessocialspend_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom18 = num0_mom18/den0_mom18
sd0_mom18

# mom 19
num0_mom19 = abs(mean(d$v72b_issuesrendaminima_wave3_imp[d$t_ind==1])- mean(d$v72b_issuesrendaminima_wave3_imp[d$t_ind==0]))
den0_mom19 = sqrt(  ( (sd(d$v72b_issuesrendaminima_wave3_imp[d$t_ind==1]))^2 + (sd(d$v72b_issuesrendaminima_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom19 = num0_mom19/den0_mom19
sd0_mom19

# mom 20
num0_mom20 = abs(mean(d$s1_sex_wave3[d$t_ind==1])- mean(d$s1_sex_wave3[d$t_ind==0]))
den0_mom20 = sqrt(  ( (sd(d$s1_sex_wave3[d$t_ind==1]))^2 + (sd(d$s1_sex_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom20= num0_mom20/den0_mom20
sd0_mom20

# mom 21
num0_mom21 = abs(mean(d$s6_education_wave2_imp[d$t_ind==1])- mean(d$s6_education_wave2_imp[d$t_ind==0]))
den0_mom21 = sqrt(  ( (sd(d$s6_education_wave2_imp[d$t_ind==1]))^2 + (sd(d$s6_education_wave2_imp[d$t_ind==0]))^2 )/2  )
sd0_mom21 = num0_mom21/den0_mom21
sd0_mom21

# mom 22
num0_mom22 = abs(mean(d$s7a_jobfixed_wave3_imp[d$t_ind==1])- mean(d$s7a_jobfixed_wave3_imp[d$t_ind==0]))
den0_mom22 = sqrt(  ( (sd(d$s7a_jobfixed_wave3_imp[d$t_ind==1]))^2 + (sd(d$s7a_jobfixed_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom22 = num0_mom22/den0_mom22
sd0_mom22

# mom 23
num0_mom23 = abs(mean(d$s7b_jobformalsector_wave1_imp[d$t_ind==1])- mean(d$s7b_jobformalsector_wave1_imp[d$t_ind==0]))
den0_mom23 = sqrt(  ( (sd(d$s7b_jobformalsector_wave1_imp[d$t_ind==1]))^2 + (sd(d$s7b_jobformalsector_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom23 = num0_mom23/den0_mom23
sd0_mom23

# mom 24
num0_mom24 = abs(mean(d$s7c_jobpublicsector_wave1_imp[d$t_ind==1])- mean(d$s7c_jobpublicsector_wave1_imp[d$t_ind==0]))
den0_mom24 = sqrt(  ( (sd(d$s7c_jobpublicsector_wave1_imp[d$t_ind==1]))^2 + (sd(d$s7c_jobpublicsector_wave1_imp[d$t_ind==0]))^2 )/2  )
sd0_mom24 = num0_mom24/den0_mom24
sd0_mom24

# mom 25
num0_mom25 = abs(mean(d$s7e_jobworry_wave3_imp[d$t_ind==1])- mean(d$s7e_jobworry_wave3_imp[d$t_ind==0]))
den0_mom25 = sqrt(  ( (sd(d$s7e_jobworry_wave3_imp[d$t_ind==1]))^2 + (sd(d$s7e_jobworry_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom25 = num0_mom25/den0_mom25
sd0_mom25

# mom 26
num0_mom26 = abs(mean(d$s10_age_wave3_imp[d$t_ind==1])- mean(d$s10_age_wave3_imp[d$t_ind==0]))
den0_mom26 = sqrt(  ( (sd(d$s10_age_wave3_imp[d$t_ind==1]))^2 + (sd(d$s10_age_wave3_imp[d$t_ind==0]))^2 )/2  )
sd0_mom26 = num0_mom26/den0_mom26
sd0_mom26

# mom 27
num0_mom27 = abs(mean(d$s14c_know3fhcparty_wave3[d$t_ind==1])- mean(d$s14c_know3fhcparty_wave3[d$t_ind==0]))
den0_mom27 = sqrt(  ( (sd(d$s14c_know3fhcparty_wave3[d$t_ind==1]))^2 + (sd(d$s14c_know3fhcparty_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom27 = num0_mom27/den0_mom27
sd0_mom27

# mom 28
num0_mom28 = abs(mean(d$crime_stronghand_wave1[d$t_ind==1])- mean(d$crime_stronghand_wave1[d$t_ind==0]))
den0_mom28 = sqrt(  ( (sd(d$crime_stronghand_wave1[d$t_ind==1]))^2 + (sd(d$crime_stronghand_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom28 = num0_mom28/den0_mom28
sd0_mom28

# mom 29
num0_mom29 = abs(mean(d$deathpenalty_reducecrime_wave1[d$t_ind==1])- mean(d$deathpenalty_reducecrime_wave1[d$t_ind==0]))
den0_mom29 = sqrt(  ( (sd(d$deathpenalty_reducecrime_wave1[d$t_ind==1]))^2 + (sd(d$deathpenalty_reducecrime_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom29 = num0_mom29/den0_mom29
sd0_mom29

# mom 30
num0_mom30 = abs(mean(d$supportdem_wave1[d$t_ind==1])- mean(d$supportdem_wave1[d$t_ind==0]))
den0_mom30 = sqrt(  ( (sd(d$supportdem_wave1[d$t_ind==1]))^2 + (sd(d$supportdem_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom30 = num0_mom30/den0_mom30
sd0_mom30

# mom 31
num0_mom31 = abs(mean(d$deathpenalty_support_wave2[d$t_ind==1])- mean(d$deathpenalty_support_wave2[d$t_ind==0]))
den0_mom31 = sqrt(  ( (sd(d$deathpenalty_support_wave2[d$t_ind==1]))^2 + (sd(d$deathpenalty_support_wave2[d$t_ind==0]))^2 )/2  )
sd0_mom31 = num0_mom31/den0_mom31
sd0_mom31

# mom 32
num0_mom32 = abs(mean(d$voteforciro_wave3[d$t_ind==1])- mean(d$voteforciro_wave3[d$t_ind==0]))
den0_mom32 = sqrt(  ( (sd(d$voteforciro_wave3[d$t_ind==1]))^2 + (sd(d$voteforciro_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom32 = num0_mom32/den0_mom32
sd0_mom32

# mom 33
num0_mom33 = abs(mean(d$voteforlula_wave3[d$t_ind==1])- mean(d$voteforlula_wave3[d$t_ind==0]))
den0_mom33 = sqrt(  ( (sd(d$voteforlula_wave3[d$t_ind==1]))^2 + (sd(d$voteforlula_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom33 = num0_mom33/den0_mom33
sd0_mom33

# mom 34
num0_mom34 = abs(mean(d$voteforserra_wave3[d$t_ind==1])- mean(d$voteforserra_wave3[d$t_ind==0]))
den0_mom34 = sqrt(  ( (sd(d$voteforserra_wave3[d$t_ind==1]))^2 + (sd(d$voteforserra_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom34 = num0_mom34/den0_mom34
sd0_mom34

# mom 35
num0_mom35 = abs(mean(d$voteforgarotinho_wave3[d$t_ind==1])- mean(d$voteforgarotinho_wave3[d$t_ind==0]))
den0_mom35 = sqrt(  ( (sd(d$voteforgarotinho_wave3[d$t_ind==1]))^2 + (sd(d$voteforgarotinho_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom35 = num0_mom35/den0_mom35
sd0_mom35

# mom 36
num0_mom36 = abs(mean(d$party_pmdb_wave3[d$t_ind==1])- mean(d$party_pmdb_wave3[d$t_ind==0]))
den0_mom36 = sqrt(  ( (sd(d$party_pmdb_wave3[d$t_ind==1]))^2 + (sd(d$party_pmdb_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom36 = num0_mom36/den0_mom36
sd0_mom36

# mom 37
num0_mom37 = abs(mean(d$party_pfl_wave3[d$t_ind==1])- mean(d$party_pfl_wave3[d$t_ind==0]))
den0_mom37 = sqrt(  ( (sd(d$party_pfl_wave3[d$t_ind==1]))^2 + (sd(d$party_pfl_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom37 = num0_mom37/den0_mom37
sd0_mom37

# mom 38
num0_mom38 = abs(mean(d$party_psdb_wave3[d$t_ind==1])- mean(d$party_psdb_wave3[d$t_ind==0]))
den0_mom38 = sqrt(  ( (sd(d$party_psdb_wave3[d$t_ind==1]))^2 + (sd(d$party_psdb_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom38 = num0_mom38/den0_mom38
sd0_mom38

# mom 39
num0_mom39 = abs(mean(d$party_pt_wave3[d$t_ind==1])- mean(d$party_pt_wave3[d$t_ind==0]))
den0_mom39 = sqrt(  ( (sd(d$party_pt_wave3[d$t_ind==1]))^2 + (sd(d$party_pt_wave3[d$t_ind==0]))^2 )/2  )
sd0_mom39 = num0_mom39/den0_mom39
sd0_mom39

# mom 40
num0_mom40 = abs(mean(d$race_white_wave1[d$t_ind==1])- mean(d$race_white_wave1[d$t_ind==0]))
den0_mom40 = sqrt(  ( (sd(d$race_white_wave1[d$t_ind==1]))^2 + (sd(d$race_white_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom40 = num0_mom40/den0_mom40
sd0_mom40

# mom 41
num0_mom41 = abs(mean(d$race_mestizo_wave1[d$t_ind==1])- mean(d$race_mestizo_wave1[d$t_ind==0]))
den0_mom41 = sqrt(  ( (sd(d$race_mestizo_wave1[d$t_ind==1]))^2 + (sd(d$race_mestizo_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom41 = num0_mom41/den0_mom41
sd0_mom41

# mom 42
num0_mom42 = abs(mean(d$race_black_wave1[d$t_ind==1])- mean(d$race_black_wave1[d$t_ind==0]))
den0_mom42 = sqrt(  ( (sd(d$race_black_wave1[d$t_ind==1]))^2 + (sd(d$race_black_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom42 = num0_mom42/den0_mom42
sd0_mom42

# mom 43
num0_mom43 = abs(mean(d$vote98_fhc_wave1[d$t_ind==1])- mean(d$vote98_fhc_wave1[d$t_ind==0]))
den0_mom43 = sqrt(  ( (sd(d$vote98_fhc_wave1[d$t_ind==1]))^2 + (sd(d$vote98_fhc_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom43 = num0_mom43/den0_mom43
sd0_mom43

# mom 44
num0_mom44 = abs(mean(d$vote98_lula_wave1[d$t_ind==1])- mean(d$vote98_lula_wave1[d$t_ind==0]))
den0_mom44 = sqrt(  ( (sd(d$vote98_lula_wave1[d$t_ind==1]))^2 + (sd(d$vote98_lula_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom44 = num0_mom44/den0_mom44
sd0_mom44

# mom 45
num0_mom45 = abs(mean(d$religion_catholic_wave1[d$t_ind==1])- mean(d$religion_catholic_wave1[d$t_ind==0]))
den0_mom45 = sqrt(  ( (sd(d$religion_catholic_wave1[d$t_ind==1]))^2 + (sd(d$religion_catholic_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom45 = num0_mom45/den0_mom45
sd0_mom45

# mom 46
num0_mom46 = abs(mean(d$religion_evangelic_wave1[d$t_ind==1])- mean(d$religion_evangelic_wave1[d$t_ind==0]))
den0_mom46 = sqrt(  ( (sd(d$religion_evangelic_wave1[d$t_ind==1]))^2 + (sd(d$religion_evangelic_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom46 = num0_mom46/den0_mom46
sd0_mom46

# mom 47
num0_mom47 = abs(mean(d$noreligion_wave1[d$t_ind==1])- mean(d$noreligion_wave1[d$t_ind==0]))
den0_mom47 = sqrt(  ( (sd(d$noreligion_wave1[d$t_ind==1]))^2 + (sd(d$noreligion_wave1[d$t_ind==0]))^2 )/2  )
sd0_mom47 = num0_mom47/den0_mom47
sd0_mom47

##################################################
# Standardized differences after matching
##################################################

# mom 1
num1_mom1 = abs(mean(d_match$v2_bairrosafe_wave1_imp[d_match$t_ind==1])- mean(d_match$v2_bairrosafe_wave1_imp[d_match$t_ind==0]))
sd1_mom1 = num1_mom1/den0_mom1
sd1_mom1

# mom 2
num1_mom2 = abs(mean(d_match$v7a_mediatvyesorno_wave3_imp[d_match$t_ind==1])- mean(d_match$v7a_mediatvyesorno_wave3_imp[d_match$t_ind==0]))
sd1_mom2 = num1_mom2/den0_mom2
sd1_mom2

# mom 3
num1_mom3 = abs(mean(d_match$v9a_mediapaperyesorno_wave3_imp[d_match$t_ind==1])- mean(d_match$v9a_mediapaperyesorno_wave3_imp[d_match$t_ind==0]))
sd1_mom3 = num1_mom3/den0_mom3
sd1_mom3

# mom 4
num1_mom4 = abs(mean(d_match$v10c_mediainternetfreq_wave3_imp[d_match$t_ind==1])- mean(d_match$v10c_mediainternetfreq_wave3_imp[d_match$t_ind==0]))
sd1_mom4 = num1_mom4/den0_mom4
sd1_mom4

# mom 5
num1_mom5 = abs(mean(d_match$v16e_conversefriends_wave3_imp[d_match$t_ind==1])- mean(d_match$v16e_conversefriends_wave3_imp[d_match$t_ind==0]))
sd1_mom5 = num1_mom5/den0_mom5
sd1_mom5

# mom 6
num1_mom6 = abs(mean(d_match$v16g_conversefamily_wave3_imp[d_match$t_ind==1])- mean(d_match$v16g_conversefamily_wave3_imp[d_match$t_ind==0]))
sd1_mom6 = num1_mom6/den0_mom6
sd1_mom6

# mom 7
num1_mom7 = abs(mean(d_match$v20_comparetobairro_wave3_imp[d_match$t_ind==1])- mean(d_match$v20_comparetobairro_wave3_imp[d_match$t_ind==0]))
sd1_mom7 = num1_mom7/den0_mom7
sd1_mom7

# mom 8
num1_mom8 = abs(mean(d_match$v30a_combat_crime_wave1[d_match$t_ind==1])- mean(d_match$v30a_combat_crime_wave1[d_match$t_ind==0]))
sd1_mom8 = num1_mom8/den0_mom8
sd1_mom8

# mom 9
num1_mom9 = abs(mean(d_match$v35a_attentionpres_wave2_imp[d_match$t_ind==1])- mean(d_match$v35a_attentionpres_wave2_imp[d_match$t_ind==0]))
sd1_mom9 = num1_mom9/den0_mom9
sd1_mom9

# mom 10
num1_mom10 = abs(mean(d_match$v36a_persuadefromothersyesorno_wave3_imp[d_match$t_ind==1])- mean(d_match$v36a_persuadefromothersyesorno_wave3_imp[d_match$t_ind==0]))
sd1_mom10 = num1_mom10/den0_mom10
sd1_mom10

# mom 11
num1_mom11 = abs(mean(d_match$v40a_thermmilitary_wave1_imp[d_match$t_ind==1])- mean(d_match$v40a_thermmilitary_wave1_imp[d_match$t_ind==0]))
sd1_mom11 = num1_mom11/den0_mom11
sd1_mom11

# mom 12
num1_mom12 = abs(mean(d_match$v40b_thermCUT_wave1_imp[d_match$t_ind==1])- mean(d_match$v40b_thermCUT_wave1_imp[d_match$t_ind==0]))
sd1_mom12 = num1_mom12/den0_mom12
sd1_mom12

# mom 13
num1_mom13 = abs(mean(d_match$v40d_thermbusiness_wave1_imp[d_match$t_ind==1])- mean(d_match$v40d_thermbusiness_wave1_imp[d_match$t_ind==0]))
sd1_mom13 = num1_mom13/den0_mom13
sd1_mom13

# mom 14
num1_mom14 = abs(mean(d_match$v41b_thermfhc_wave3_imp[d_match$t_ind==1])- mean(d_match$v41b_thermfhc_wave3_imp[d_match$t_ind==0]))
sd1_mom14 = num1_mom14/den0_mom14
sd1_mom14

# mom 15
num1_mom15 = abs(mean(d_match$v43a_pidyesorno_wave3[d_match$t_ind==1])- mean(d_match$v43a_pidyesorno_wave3[d_match$t_ind==0]))
sd1_mom15 = num1_mom15/den0_mom15
sd1_mom15

# mom 16
num1_mom16 = abs(mean(d_match$v46_pidimportant_wave1_imp[d_match$t_ind==1])- mean(d_match$v46_pidimportant_wave1_imp[d_match$t_ind==0]))
sd1_mom16 = num1_mom16/den0_mom16
sd1_mom16

# mom 17
num1_mom17 = abs(mean(d_match$v50_ideology_wave3_imp[d_match$t_ind==1])- mean(d_match$v50_ideology_wave3_imp[d_match$t_ind==0]))
sd1_mom17 = num1_mom17/den0_mom17
sd1_mom17

# mom 18
num1_mom18 = abs(mean(d_match$v69a_issuessocialspend_wave3_imp[d_match$t_ind==1])- mean(d_match$v69a_issuessocialspend_wave3_imp[d_match$t_ind==0]))
sd1_mom18 = num1_mom18/den0_mom18
sd1_mom18

# mom 19
num1_mom19 = abs(mean(d_match$v72b_issuesrendaminima_wave3_imp[d_match$t_ind==1])- mean(d_match$v72b_issuesrendaminima_wave3_imp[d_match$t_ind==0]))
sd1_mom19 = num1_mom19/den0_mom19
sd1_mom19

# mom 20
num1_mom20 = abs(mean(d_match$s1_sex_wave3[d_match$t_ind==1])- mean(d_match$s1_sex_wave3[d_match$t_ind==0]))
sd1_mom20 = num1_mom20/den0_mom20
sd1_mom20

# mom 21
num1_mom21 = abs(mean(d_match$s6_education_wave2_imp[d_match$t_ind==1])- mean(d_match$s6_education_wave2_imp[d_match$t_ind==0]))
sd1_mom21 = num1_mom21/den0_mom21
sd1_mom21

# mom 22
num1_mom22 = abs(mean(d_match$s7a_jobfixed_wave3_imp[d_match$t_ind==1])- mean(d_match$s7a_jobfixed_wave3_imp[d_match$t_ind==0]))
sd1_mom22 = num1_mom22/den0_mom22
sd1_mom22

# mom 23
num1_mom23 = abs(mean(d_match$s7b_jobformalsector_wave1_imp[d_match$t_ind==1])- mean(d_match$s7b_jobformalsector_wave1_imp[d_match$t_ind==0]))
sd1_mom23 = num1_mom23/den0_mom23
sd1_mom23

# mom 24
num1_mom24 = abs(mean(d_match$s7c_jobpublicsector_wave1_imp[d_match$t_ind==1])- mean(d_match$s7c_jobpublicsector_wave1_imp[d_match$t_ind==0]))
sd1_mom24 = num1_mom24/den0_mom24
sd1_mom24

# mom 25
num1_mom25 = abs(mean(d_match$s7e_jobworry_wave3_imp[d_match$t_ind==1])- mean(d_match$s7e_jobworry_wave3_imp[d_match$t_ind==0]))
sd1_mom25 = num1_mom25/den0_mom25
sd1_mom25

# mom 26
num1_mom26 = abs(mean(d_match$s10_age_wave3_imp[d_match$t_ind==1])- mean(d_match$s10_age_wave3_imp[d_match$t_ind==0]))
sd1_mom26 = num1_mom26/den0_mom26
sd1_mom26

# mom 27
num1_mom27 = abs(mean(d_match$s14c_know3fhcparty_wave3[d_match$t_ind==1])- mean(d_match$s14c_know3fhcparty_wave3[d_match$t_ind==0]))
sd1_mom27 = num1_mom27/den0_mom27
sd1_mom27

# mom 28
num1_mom28 = abs(mean(d_match$crime_stronghand_wave1[d_match$t_ind==1])- mean(d_match$crime_stronghand_wave1[d_match$t_ind==0]))
sd1_mom28 = num1_mom28/den0_mom28
sd1_mom28

# mom 29
num1_mom29= abs(mean(d_match$deathpenalty_reducecrime_wave1[d_match$t_ind==1])- mean(d_match$deathpenalty_reducecrime_wave1[d_match$t_ind==0]))
sd1_mom29 = num1_mom29/den0_mom29
sd1_mom29

# mom 30
num1_mom30 = abs(mean(d_match$supportdem_wave1[d_match$t_ind==1])- mean(d_match$supportdem_wave1[d_match$t_ind==0]))
sd1_mom30 = num1_mom30/den0_mom30
sd1_mom30

# mom 31
num1_mom31 = abs(mean(d_match$deathpenalty_support_wave2[d_match$t_ind==1])- mean(d_match$deathpenalty_support_wave2[d_match$t_ind==0]))
sd1_mom31 = num1_mom31/den0_mom31
sd1_mom31

# mom 32
num1_mom32 = abs(mean(d_match$voteforciro_wave3[d_match$t_ind==1])- mean(d_match$voteforciro_wave3[d_match$t_ind==0]))
sd1_mom32 = num1_mom32/den0_mom32
sd1_mom32

# mom 33
num1_mom33 = abs(mean(d_match$voteforlula_wave3[d_match$t_ind==1])- mean(d_match$voteforlula_wave3[d_match$t_ind==0]))
sd1_mom33 = num1_mom33/den0_mom33
sd1_mom33

# mom 34
num1_mom34 = abs(mean(d_match$voteforserra_wave3[d_match$t_ind==1])- mean(d_match$voteforserra_wave3[d_match$t_ind==0]))
sd1_mom34 = num1_mom34/den0_mom34
sd1_mom34

# mom 35
num1_mom35 = abs(mean(d_match$voteforgarotinho_wave3[d_match$t_ind==1])- mean(d_match$voteforgarotinho_wave3[d_match$t_ind==0]))
sd1_mom35 = num1_mom35/den0_mom35
sd1_mom35

# mom 36
num1_mom36 = abs(mean(d_match$party_pmdb_wave3[d_match$t_ind==1])- mean(d_match$party_pmdb_wave3[d_match$t_ind==0]))
sd1_mom36 = num1_mom36/den0_mom36
sd1_mom36

# mom 37
num1_mom37 = abs(mean(d_match$party_pfl_wave3[d_match$t_ind==1])- mean(d_match$party_pfl_wave3[d_match$t_ind==0]))
sd1_mom37 = num1_mom37/den0_mom37
sd1_mom37

# mom 38
num1_mom38 = abs(mean(d_match$party_psdb_wave3[d_match$t_ind==1])- mean(d_match$party_psdb_wave3[d_match$t_ind==0]))
sd1_mom38 = num1_mom38/den0_mom38
sd1_mom38

# mom 39
num1_mom39 = abs(mean(d_match$party_pt_wave3[d_match$t_ind==1])- mean(d_match$party_pt_wave3[d_match$t_ind==0]))
sd1_mom39 = num1_mom39/den0_mom39
sd1_mom39

# mom 40
num1_mom40 = abs(mean(d_match$race_white_wave1[d_match$t_ind==1])- mean(d_match$race_white_wave1[d_match$t_ind==0]))
sd1_mom40 = num1_mom40/den0_mom40
sd1_mom40

# mom 41
num1_mom41 = abs(mean(d_match$race_mestizo_wave1[d_match$t_ind==1])- mean(d_match$race_mestizo_wave1[d_match$t_ind==0]))
sd1_mom41 = num1_mom41/den0_mom4
sd1_mom41

# mom 43
num1_mom42 = abs(mean(d_match$race_black_wave1[d_match$t_ind==1])- mean(d_match$race_black_wave1[d_match$t_ind==0]))
sd1_mom42 = num1_mom42/den0_mom42
sd1_mom42

# mom 44
num1_mom43 = abs(mean(d_match$vote98_fhc_wave1[d_match$t_ind==1])- mean(d_match$vote98_fhc_wave1[d_match$t_ind==0]))
sd1_mom43 = num1_mom43/den0_mom43
sd1_mom43

# mom 44
num1_mom44 = abs(mean(d_match$vote98_lula_wave1[d_match$t_ind==1])- mean(d_match$vote98_lula_wave1[d_match$t_ind==0]))
sd1_mom44 = num1_mom44/den0_mom44
sd1_mom44

# mom 45
num1_mom45 = abs(mean(d_match$religion_catholic_wave1[d_match$t_ind==1])- mean(d_match$religion_catholic_wave1[d_match$t_ind==0]))
sd1_mom45 = num1_mom45/den0_mom45
sd1_mom45

# mom 46
num1_mom46 = abs(mean(d_match$religion_evangelic_wave1[d_match$t_ind==1])- mean(d_match$religion_evangelic_wave1[d_match$t_ind==0]))
sd1_mom46 = num1_mom46/den0_mom46
sd1_mom46

# mom 47
num1_mom47 = abs(mean(d_match$noreligion_wave1[d_match$t_ind==1])- mean(d_match$noreligion_wave1[d_match$t_ind==0]))
sd1_mom47 = num1_mom47/den0_mom47
sd1_mom47

##################
# Generate data
##################

colnames = c("Perceptions of safety", #mom1
             "Do you watch TV news?", #mom2
             "Do you read about politics in newspapers", #mom3
             "Frequency of internet usage", #mom4
             "Talk about politics with friends", #mom5
             "Talk about politics with family", #mom6
             "Comparison with other families", #mom7
             "Importance of combating crime", #mom8
             "Attention paid to presidential election", #mom9
             "Have you persuaded others to vote?", #mom10
             "Military feeling thermometer", #mom11
             "Union feeling thermometer", #mom12
             "Business sector feeling thermometer", #mom13
             "President (FHC) feeling thermometer", #mom14
             "Do you identify with a party?", #mom15
             "Importance of party when you vote", #mom16
             "Ideology", #mom17
             "Opinions about social spending", #mom18
             "Opinions about minimum wage", #mom19
             "Gender", #mom20
             "Education", #mom21
             "Stable job", #mom22
             "Job in the formal sector", #mom23
             "Job in the public sector", #mom24
             "Worried about losing job in the future", #mom25
             "Age", #mom26
             "Name of one presidential candidate", #mom27
             "Support strong-arm policies to reduce crime", #mom28
             "Support death penalty to reduce crime", #mom29
             "Support for democracy", #mom30
             "Support for death penalty", #mom31
             "Vote for Ciro", #mom32
             "Vote for Lula", #mom33
             "Vote for Serra", #mom34
             "Vote for Garotinho", #mom35
             "Do you identify with the PMDB", #mom36
             "Do you identify with the PFL", #mom37
             "Do you identify with the PSDB", #mom38
             "Do you identify with the PT", #mom39
             "White", #mom40
             "Pardo/Mestizo", #mom41
             "Black", #mom42
             "Voted for FHC in 1998", #mom43
             "Voted for Lula in 1998", #mom44
             "Catholic", #mom45
             "Evangelical", #mom46
             "No religion") #mom47

sd0 =  c(sd0_mom1,
         sd0_mom2,
         sd0_mom3,
         sd0_mom4,
         sd0_mom5,
         sd0_mom6,
         sd0_mom7,
         sd0_mom8,
         sd0_mom9,
         sd0_mom10,
         sd0_mom11,
         sd0_mom12,
         sd0_mom13,
         sd0_mom14,
         sd0_mom15,
         sd0_mom16,
         sd0_mom17,
         sd0_mom18,
         sd0_mom19,
         sd0_mom20,
         sd0_mom21,
         sd0_mom22,
         sd0_mom23,
         sd0_mom24,
         sd0_mom25,
         sd0_mom26,
         sd0_mom27,
         sd0_mom28,
         sd0_mom29,
         sd0_mom30,
         sd0_mom31,
         sd0_mom32,
         sd0_mom33,
         sd0_mom34,
         sd0_mom35,
         sd0_mom36,
         sd0_mom37,
         sd0_mom38,
         sd0_mom39,
         sd0_mom40,
         sd0_mom41,
         sd0_mom42,
         sd0_mom43,
         sd0_mom44,
         sd0_mom45,
         sd0_mom46,
         sd0_mom47)

sd1 =  c(sd1_mom1,
         sd1_mom2,
         sd1_mom3,
         sd1_mom4,
         sd1_mom5,
         sd1_mom6,
         sd1_mom7,
         sd1_mom8,
         sd1_mom9,
         sd1_mom10,
         sd1_mom11,
         sd1_mom12,
         sd1_mom13,
         sd1_mom14,
         sd1_mom15,
         sd1_mom16,
         sd1_mom17,
         sd1_mom18,
         sd1_mom19,
         sd1_mom20,
         sd1_mom21,
         sd1_mom22,
         sd1_mom23,
         sd1_mom24,
         sd1_mom25,
         sd1_mom26,
         sd1_mom27,
         sd1_mom28,
         sd1_mom29,
         sd1_mom30,
         sd1_mom31,
         sd1_mom32,
         sd1_mom33,
         sd1_mom34,
         sd1_mom35,
         sd1_mom36,
         sd1_mom37,
         sd1_mom38,
         sd1_mom39,
         sd1_mom40,
         sd1_mom41,
         sd1_mom42,
         sd1_mom43,
         sd1_mom44,
         sd1_mom45,
         sd1_mom46,
         sd1_mom47)

Comparison = rep("Matched treated-Matched control",47)    

load("~/Dropbox/Crime Brazil/02_analysis/002_representativeness.Rdata")

names(meanbalance0)
meanbalance0

meanbalance1 = data.frame(colnames,sd1,Comparison)
meanbalance1$order <- nrow(meanbalance1):1
meanbalance1 <-meanbalance1[order(meanbalance1$order),]
meanbalance1$colnames <- factor(meanbalance1$colnames, levels = meanbalance1$colnames[order(meanbalance1$order)])
meanbalance1
names(meanbalance1)

colnames(meanbalance1) = c("covariate","sd","Comparison","order")

colnames(meanbalance1)
colnames(meanbalance0)

meanbalance = rbind(meanbalance0,meanbalance1)
meanbalance = data.frame(meanbalance)
meanbalance

#############################
# Generate plot
#############################

# mean balance
loveplot = ggplot(data = meanbalance, mapping = aes(x = covariate, y = sd, group = Comparison, color = Comparison)) +
  geom_hline(yintercept = 0.2, color = "black", size = 1, linetype="dashed") +
  geom_hline(yintercept = 0.1, color = "gray", size = 1, linetype="dashed") +
  coord_flip() +
  theme_bw() + 
  theme(legend.key = element_blank()) +
  labs(y = "Absolute standardized differences") +
  labs(x = "") +
  scale_y_continuous( limits = c(0,0.3), breaks = c(0,0.1,0.2,0.3) ) +
  scale_color_manual(values=c("gray50","black")) + 
  geom_point(aes(shape=Comparison), size=4) +       
  scale_shape_manual(values=c(8,16)) + 
  theme(text = element_text(size=20))

loveplot


